using System;
using System.Collections.Generic;

namespace LFramework 
{
    /// <summary>
    /// 计时管理器
    /// </summary>
    public class TimeManager : IDisposable
    {
        private List<TimerBase> m_RunningTimer = new List<TimerBase>();
        private List<TimerBase> m_FinishedTimer = new List<TimerBase>();

        private List<int> m_RemoveID = new List<int>();
        private int m_GenID = 0;

        public void OnUpdate(float dt)
        {
            if (m_RemoveID.Count > 0)
            {
                for (int i = 0; i < m_RemoveID.Count; i++)
                {
                    int id = m_RemoveID[i];

                    int index = FindRunningTimer(id);
                    if (index >= 0)
                    {
                        m_RunningTimer[id].Release();
                        m_RunningTimer.RemoveAt(index);
                    }
                }

                m_RemoveID.Clear();
            }

            if (m_RunningTimer.Count == 0)
                return;

            for (int i = 0; i < m_RunningTimer.Count; i++)
            {
                m_RunningTimer[i].Update(dt);
                if (m_RunningTimer[i].IsFinished)
                {
                    m_FinishedTimer.Add(m_RunningTimer[i]);
                }
            }

            if (m_FinishedTimer.Count > 0)
            {
                for (int i = 0; i < m_FinishedTimer.Count; i++)
                {
                    m_RunningTimer[i].Release();
                    m_RunningTimer.Remove(m_FinishedTimer[i]);
                }

                m_FinishedTimer.Clear();
            }
        }

        private int GenID()
        {
            return ++m_GenID;
        }

        public int GenClockTimer(float total_time,Action finish_cb)
        {
            ClockTimer ct = ClockTimer.Create(GenID(), finish_cb, total_time);
            m_RunningTimer.Add(ct);
            return ct.ID;
        }

        public int GenFrameTimer(int frame, Action cb)
        {
            FrameTimer ft = FrameTimer.Create(GenID(), frame, cb);
            m_RunningTimer.Add(ft);
            return ft.ID;
        }

        public int GenLoopTimer(float loop_time, Action cb)
        {
            LoopTimer lt = LoopTimer.Create(GenID(), loop_time, cb);
            m_RunningTimer.Add(lt);
            return lt.ID;
        }

        public void RestartTimer(int id)
        {
            TimerBase tb = null;
            foreach (var obj in m_RunningTimer)
            {
                if (obj.ID == id)
                {
                    tb = obj;
                    break;
                }
            }

            if (tb != null)
            {
                tb.Restart();
            }
        }

        public void ReleaseTimer(int id)
        {
            m_RemoveID.Add(id);
        }

        private int FindRunningTimer(int id)
        {
            for (int i = 0; i < m_RunningTimer.Count; i++)
            {
                if (m_RunningTimer[i].ID == id)
                    return i;
            }

            return -1;
        }

        public void Dispose()
        {
            
        }
    }
}
